package edu.worldskills.b;

import android.annotation.SuppressLint;
import  android.graphics.Canvas;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class SimpleStickyHeaderItemDecoration extends RecyclerView.ItemDecoration {

    private final HeaderProvider headerProvider;

    public interface HeaderProvider {
        View getHeaderView(int position);
    }

    public SimpleStickyHeaderItemDecoration(HeaderProvider headerProvider) {
        this.headerProvider = headerProvider;
    }

    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDrawOver(c, parent, state);

        int childCount = parent.getChildCount();
        if (childCount <= 0 || parent.getAdapter() == null) {
            return;
        }

        int firstVisiblePosition = parent.getChildAdapterPosition(parent.getChildAt(0));
        int headerPosition = findHeaderPosition(firstVisiblePosition);
        if (headerPosition < 0) {
            return;
        }

        View headerView = headerProvider.getHeaderView(headerPosition);
        if (headerView == null) {
            return;
        }

        // 测量 HeaderView
        int widthSpec = View.MeasureSpec.makeMeasureSpec(parent.getWidth(), View.MeasureSpec.EXACTLY);
        @SuppressLint("Range") int heightSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.UNSPECIFIED);
        headerView.measure(widthSpec, heightSpec);
        headerView.layout(0, 0, headerView.getMeasuredWidth(), headerView.getMeasuredHeight());

        // 绘制 HeaderView
        c.save();
        c.translate(0, 0); // 直接绘制在顶部
        headerView.draw(c);
        c.restore();
    }

    private int findHeaderPosition(int firstVisiblePosition) {
        for (int i = firstVisiblePosition; i >= 0; i--) {
            if (isHeader(i)) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int position = parent.getChildAdapterPosition(view);
        if (position != RecyclerView.NO_POSITION && isHeader(position)) {
            View headerView = headerProvider.getHeaderView(position);
            if(headerView != null) {
                int widthSpec = View.MeasureSpec.makeMeasureSpec(parent.getWidth(), View.MeasureSpec.EXACTLY);
                int heightSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.UNSPECIFIED);
                headerView.measure(widthSpec, heightSpec);
                outRect.top = headerView.getMeasuredHeight();
            }
        }
    }

    private boolean isHeader(int position) {
        return headerProvider.getHeaderView(position) != null;
    }
}